﻿using CommonType;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace S_UserInfor.Infrasturctre
{
    public class BaceRepository<T> : IRepository<T> where T : class, new()
    {
        private readonly ISqlSugarClient _client;
        public BaceRepository(ISqlSugarClient client)
        {
            _client = client;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T model)
        {   //ExecuteCommandAsync返回受影响行数
            var res = await _client.Insertable(model).ExecuteCommandAsync();
            return res;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T t)
        {
            var res = await _client.Deleteable(t).ExecuteCommandAsync();
            return res;
        }
        /// <summary>
        /// 多条件查询
        /// </summary>
        /// <param name="expre"></param>
        /// <returns></returns>
        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> expre)
        {
            return await _client.Queryable<T>().Where(expre).ToListAsync();
        }

        /// <summary>
        /// 单个查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<T> GetFindAsync(int id)
        {
            return await _client.Queryable<T>().InSingleAsync(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T model)
        {
            var res = await _client.Updateable(model).ExecuteCommandAsync();
            return res;
        }
    }
}
